<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

<html>

<head>

   <title>AnkiDroid Code Conventions</title>
   <meta name="Author" content="AnkiDroid">
   <meta name="Keywords" content="java, programming, code, style, guideline, guidelines, recommendation, recommendations, layout, standard, conventions, standardization, naming, names, indentation, block, readability, maintainability, mixed case, suffix, prefix, statement, statements, conditional, white space, align, alignment comments, javadoc, code complete, steve mcconnel, software, services">
   <meta name="Description"
         content="Android Programming Style Guidelines. Coding recommensations for Android developers including naming conventions, code layout, commenting and more.">

   <meta http-equiv="Content-Type"
         content="text/html; charset=iso-8859-1">
   <link rel=stylesheet href="style.css" type="text/css">
</head>


<body>

<table cellpadding=10 border=0 cellspacing=0>
<tr>

<p>


<!--
*******************************************************************************
   HEADING
*******************************************************************************
-->

<table width="100%">
  <tr>
    <td width="90%">
      <center>
      <h1>AnkiDroid Code Conventions</h1>

      <em> Version 1.0, September 2010        </em><br>
      <em> AnkiDroid </em><br>

      <em> Copyright &copy; 2010</em><br>

      <p>
      This document is available for download at <a href="http://code.google.com/p/ankidroid/downloads/list">http://code.google.com/p/ankidroid/downloads/list</a>
      <a href=""></a>
      </center>
      </td>

    <td width="10%">
      <img src="anki.png" alt="AnkiDroid" align="right" vspace=10 hspace=30>
      </td>
  </tr>
</table>

<p>

<hr width="100%">



<!--
*******************************************************************************
   TABLE OF CONTENT
*******************************************************************************
-->

<h1>
Table of Content
</h1>

<ul>
<li><a href="#introduction">
  1 Introduction</a>
  <ul>
    <li><a href="#Layout of the Recommendations">
    1.1 Layout of the Recommendations</a></li>

    <li><a href="#Recommendation">
    1.2 Recommendations Importance</a></li>
    <li><a href="#Checkstyle and Formatter">
    1.3 Automatic Style Checking and Formatting</a></li>
    </ul>
    </li>

<li><a href="#General Recommendations">
  2 General Recommendations</a></li>
    
<li><a href="#Naming Conventions">
  3 Naming Conventions</a>
  <ul>
    <li><a href="#General">
    3.1 General Naming Conventions</a></li>
    <li><a href="#Specific">
    3.2 Specific Naming Conventions</a></li>
    </ul>
    </li>

<li><a href="#Layout Techniques">
  4 Layout Techniques</a>
  <ul>
    <li><a href="#Length Line">
    4.1 Length Line</a></li>
    <li><a href="#Indentation">
    4.2 Indentation</a></li>
    <li><a href="#Braces">
    4.3 Braces</a></li>
    <li><a href="#White Spaces">
    4.4 White Spaces</a></li>
    <li><a href="#Blank Lines">
    4.5 Blank Lines</a></li>
  </ul>
  </li>
    
<li><a href="#Control Structures">
  5 Control Structures</a>
  <ul>
    <li><a href="#if">
    5.1 if</a></li>
    <li><a href="#for">
    5.2 for</a></li>
    <li><a href="#while">
    5.3 while</a></li>
    <li><a href="#do-while">
    5.4 do-while</a></li>
    <li><a href="#switch">
    5.5 switch</a></li>
    <li><a href="#try-catch">
    5.6 try-catch</a></li>
  </ul>
  </li>

<li><a href="#Comments">
  6 Comments</a></li>

<li><a href="#Logging">
  7 Logging</a></li>
  
<li><a href="#File Organization">
  8 File Organization</a></li>
  
<li><a href="#Miscellaneous">
  9 Miscellaneous</a></li>
    <ul>
    <li><a href="#general">
    9.1 General</a></li>
    <li><a href="#types">
    9.2 Types</a></li>
    <li><a href="#variables and constants">
    9.3 Variables and Constants</a></li>
    <li><a href="#operators">
    9.4 Operators</a></li>
    <li><a href="#conditionals">
    9.5 Conditionals</a></li>
    <li><a href="#loops">
    9.6 Loops</a></li>
    <li><a href="#exceptions and finalizers">
    9.7 Exceptions and Finalizers</a></li>
    <li><a href="#android xml layouts">
    9.8 Android XML Layouts</a></li>
  </ul>
  
<li><a href="#Cheat Sheet">
  10 Cheat Sheet</a></li>
  
<li><a href="#References">
  11 References</a></li>

  </ul>

<hr width="100%">



<!--
*******************************************************************************

   1 INTRODUCTION

*******************************************************************************
-->


<h1>
<a name="introduction"></a>1 Introduction
</h1>
This document lists the recommendations applied to AnkiDroid, an Android project based on Anki.
<p>
<!--
The recommendations are based on individual experience and on the next coding standards: 
<a href="#ref1">[1]</a>,
<a href="#ref2">[2]</a>,
<a href="#ref3">[3]</a>,
<a href="#ref4">[4]</a>,
<a href="#ref5">[5]</a> and
<a href="#ref6">[6]</a>.
<br/><br/>
-->
If you find any error on this document or any inconsistency between the recommendations presented here and the automatic checkstyle and formatter, please send an email to <a href="mailto:ankidroid@gmail.com">ankidroid@gmail.com</a>.

<h3>
<a name="Layout of the Recommendations"></a>1.1 Layout of the Recommendations
</h3>

<p>
Layout for the recommendations is as follows:

<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
n. Guideline short description
</td>
</tr>

<tr>

<td class="example">Example if applicable
</td>
</tr>

<!--
<tr>
<td class="rationale">
Motivation, background and additional information.
</td>
</tr>
-->
</table>


<h3>
<a name="Recommendation"></a>1.2 Recommendations Importance
</h3>

In this document the words <em>must</em>, <em>should</em> and <em>can</em> are used to convey the importance of a recommendation. A <em>must</em> requeriment have to be followed, a <em>should</em> is a strong recommendation and a <em>can</em> is a general suggestion.
 
<h3>
<a name="Checkstyle and Formatter"></a>1.3 Automatic Style Checking and Formatting
</h3>

In order to benefit from automatic code style checking, following the recommendations below, install the <em>Checkstyle</em> plugin in your IDE and use this configuration file: <a href="http://ankidroid.googlecode.com/files/ankidroid.checkstyle.xml">ankidroid.checkstyle.xml</a>

<br/><br/>

In Eclipse, if you would like to format automatically the code, download the configuration files for <em>Clean Up</em>, <em>Formatter</em> and <em>Organize Imports</em> and import them to Eclipse. These files can be found here: <a href="http://ankidroid.googlecode.com/files/AnkiDroid%20Formatter%2C%20Clean%20Up%20and%20Import%20Order.zip">AnkiDroid Formatter, Clean Up and Import Order.zip</a>

<!--
*******************************************************************************

   GENERAL RECOMMENDATIONS

*******************************************************************************
-->


<p>
<h1>
<a name="General Recommendations"></a>2 General Recommendations
</h1>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
1. Any violation to the guide is allowed if it enhances one of the following, by order of importance (higher levels cannot be sacrificed for lower levels):
</td>
</tr>

<tr>
<td class="example">- Logical Structure
- Consistency
- Readability
- Ease of modifications
</td>
</tr>

</table>


<!--
*******************************************************************************

   NAMING CONVENSIONS

*******************************************************************************
-->

<p>
<h1>
<a name="Naming Conventions"></a>3 Naming Conventions

</h1>

<h3>
<a name="General"></a>3.1 General Naming Conventions
</h3>




<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
2. All names should be written in English.
</td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
3. Package names should be in all lower case.
</td>
</tr>

<tr>
<td class="example">com.company.application.ui
com.sun.eng
edu.cmu.cs.bovik.cheese
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
4. Class names must be nouns, in mixed case with the first letter of each internal word capitalized.
</td>
</tr>

<tr>
<td class="example">class Line;
class AudioSystem;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
5. Variable names must be in mixed case starting with lower case.
</td>
</tr>

<tr>
<td class="example">int age;
float availableWidth;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
6. Non-public, non-static field names should start with m.
</td>
</tr>

<tr>
<td class="example">private long mLongVariable;
private int mIntVariable;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
7. Static field names should start with s.
</td>
</tr>

<tr>
<td class="example">private static MyClass sSingleton;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
8. Constant (final variables) names must be all uppercase using underscore to separate words.
</td>
</tr>

<tr>
<td class="example">public static final int SOME_CONSTANT = 42;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
9. Associated constants (final variables) should be prefixed by a common type name.
</td>
</tr>

<tr>
<td class="example">public static final int COLOR_RED = 1;
public static final int COLOR_GREEN = 2;
public static final int COLOR_BLUE = 3; 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
10. Method names should be verbs in mixed case, with the first letter in lowercase and with the first letter of each internal word capitalized.
</td>
</tr>

<tr>
<td class="example">getName();
computeTotalWidth();
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
11. Functions (methods with a return) should be named after what they return and procedures (<em>void</em> methods) after what they do.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
12. In a method name, the name of the object is implicit and should be avoided.
</td>
</tr>

<tr>
<td class="example">employee.getName();  <i class="not">// NOT: employee.getEmployeeName();</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
13. Negated boolean variable names must be avoided.
</td>
</tr>

<tr>
<td class="example">boolean isLoaded;  <i class="not">// NOT: boolean isNotLoaded;</i>
boolean isError;   <i class="not">// NOT: boolean isNotError;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
14. Abbreviations in names should be avoided.
</td>
</tr>

<tr>
<td class="example">computeAverage();               <i class="not">// NOT: compAvg();</i>
ActionEvent event;              <i class="not">// NOT: ActionEvent e;</i>
catch (Exception exception) {   <i class="not">// NOT: catch (Exception e) {</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
15. Abbreviations and acronyms should not be uppercase when used in a name.
</td>
</tr>

<tr>
<td class="example">getCustomerId();     <i class="not">// NOT: getCustomerID();</i>
exportHtmlSource();  <i class="not">// NOT: exportHTMLSource();</i> 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
16. Generic variables should have the same name as their type.
</td>
</tr>

<tr>
<td class="example">void setView(View view);        <i class="not">// NOT: void setView(View v);</i>
                                <i class="not">// NOT: void setView(View aView);</i>
                              
void close(Database database);  <i class="not">// NOT: void close(Database db);</i>
                                <i class="not">// NOT: void close(Database sqliteDB);</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
17. The terms <em>get/set</em> must be used where a class attribute is accessed directly.
</td>
</tr>

<tr>
<td class="example">author.getName();
author.setName(name);

point.getX();
point.setX(3);
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
18. <em>is</em> prefix should be used for boolean variables and methods. Alternatively and if it fits better, <em>has</em>, <em>can</em> and <em>should</em> prefixes can be used.
</td>
</tr>

<tr>
<td class="example">boolean isVisible;
boolean isOpen();
boolean hasLicense();
boolean canEvaluate();
boolean shouldAbort = false;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
19. The term <em>compute</em> can be used in methods where something is computed.
</td>
</tr>

<tr>
<td class="example">valueSet.computeAverage();
matrix.computeInverse();
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
20. The term <em>find</em> can be used in methods where something is looked up.
</td>
</tr>

<tr>
<td class="example">vertex.findNearestVertex();
matrix.findSmallestElement();
node.findShortestPath(Node destinationNode); 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
21. The term <em>initialize</em> can be used where an object or a concept is set up.
</td>
</tr>

<tr>
<td class="example">initializeViews();
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
22. Variables with a large scope should have very descriptive (and usually long) names, variables with a small scope can have short names.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
23. Iterator variables can be called i, j, k, m, n...
</td>
</tr>

<tr>
<td class="example">for (int i = 0; i < downloads.size(); i++) {
    statements; 
} 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
24. Plural form should be used on names representing a collection of objects.
</td>
</tr>

<tr>
<td class="example">ArrayList<Download> downloads;
int[] points;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
25. <em>num</em> prefix should be used for variables representing a number of objects.
</td>
</tr>

<tr>
<td class="example">int numPoints = points.length();
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
26. <em>Number</em> suffix should be used for variables representing an entity number.
</td>
</tr>

<tr>
<td class="example">int employeeNumber;
int comicNumber;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
27. Exception classes should have <em>Exception</em> like a suffix.
</td>
</tr>

<tr>
<td class="example">class CustomException extends Exception {
    ... 
} 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
28. Singleton classes should return their unique instance through the <em>getInstance</em> method.
</td>
</tr>

<tr>
<td class="example">class MySingletonClass {
  private final static MySingletonClass sInstance = new MySingletonClass();

  private MySingletonClass() {
      ...
  }

  public static MySingletonClass getInstance() {  <i class="not">// NOT: get() or instance()...</i>
      return sInstance;
  }
}
</td>
</tr>

</table>


<h3>
<a name="Specific"></a>3.1 Specific Naming Conventions
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
29. String key resources must be all lowercase using underscore to separate words.
</td>
</tr>

<tr>
<td class="example">&lt;string name="good_example_key"&gt;Example value&lt;/string&gt;   <i class="not">// NOT: &lt;string name="badExampleKey">Example value&lt;/string&gt;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
30. XML elements identifiers must be all lowercase using underscore to separate words.
</td>
</tr>

<tr>
<td class="example">&lt;TextView android:id="@+id/good_id_example"              <i class="not">// NOT: &lt;TextView android:id="@+id/badIdExample"</i>
    android:layout_width="wrap_content"                              <i class="not">android:layout_width="wrap_content"</i>
    android:layout_height="wrap_content"                             <i class="not">android:layout_height="wrap_content"</i>
    /&gt;                                                               <i class="not">/&gt;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
31. Activiy names can have <em>Activity</em> like a suffix.
</td>
</tr>

<tr>
<td class="example">public class ExampleActivity extends Activity {
    ...
}
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%" >

<tr>
<td class="recom">
32. Test method names should be composed by a name representing what is being tested and a name stating which specific case is being tested, separated with underscore.
</td>
</tr>

<tr>
<td class="example">testMethod_specificCase1
testMethod_specificCase2

void testIsDistinguishable_protanopia() {
    ColorMatcher colorMatcher = new ColorMatcher(PROTANOPIA)
    assertFalse(colorMatcher.isDistinguishable(Color.RED, Color.BLACK))
    assertTrue(colorMatcher.isDistinguishable(Color.X, Color.Y))
}
</td>
</tr>

</table>


<!--
*******************************************************************************

   LAYOUT TECHNIQUES

*******************************************************************************
-->

<p>
<h1>
<a name="Layout Techniques"></a>4 Layout Techniques

</h1>

<h3>
<a name="Length Line"></a>4.1 Length Line
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">

<tr>
<td class="recom">
33. File content must be kept within 120 columns.
</td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>


<h3>
<a name="Indentation"></a>4.2 Indentation
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">

<tr>
<td class="recom">
34. Basic indentation should be 4 spaces, without using tabs.
</td>
</tr>

<tr>
<td class="example">if (condition) {
    statements;
    ...
}
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">

<tr>
<td class="recom">
35. Indentation of any wrapping line should be 8 spaces, without using tabs.
</td>
</tr>

<tr>
<td class="example">if ((condition1 && condition2)
        || (condition3 && condition4)
        ||!(condition5 && condition6)) {
    doSomethingAboutIt();
}
</td>
</tr>

</table>
<h3>

<h3>
<a name="Braces"></a>4.3 Braces
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
36. 1TBS (One True Brace Style) must be used. That means:<br />
- Opening brace "{" appears at the end of the same line as the declaration statement. <br>
- Ending brace "}" takes up an entire line by itself and it is intended at the same level that its correspondent opening statement. <br>
- Braces are mandatory, even for single-statements or empty blocks.
</tr>

<tr>
<td class="example">class MyClass {
    int func() {
        if (something) {
            // ...
        } else if (somethingElse) {
            // ...
        } else {
            // ...
        }
    }
}

<i class="not">// NOT:
if (condition) body();

if (condition)
    body();</i>
</td>
</tr>

</table>


<a name="White Spaces"></a>4.4 White Spaces
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
37. White space should be used in the following cases:<br />
- After and before operators. <br>
- Before an opening brace. <br>
- After Java reserved words. <br>
- After commas. <br>
- After semicolons in for statements.<br>
- After any comment identifier.<br>
</td>
</tr>

<tr>
<td class="example">a = (b + c) * d; <i class="not">// NOT: a=(b+c)*d</i>

if (true) {  <i class="not">// NOT: if (true){</i>
    ...

while (true) {   <i class="not">// NOT: while(true) {</i>
    ...

doSomething(a, b, c, d);  <i class="not">// NOT: doSomething(a,b,c,d);</i>

for (i = 0; i &lt; 10; i++) {  <i class="not">// NOT: for(i=0;i&lt;10;i++){</i>
    ...

// This is a comment  <i class="not">// NOT: //This is a comment</i>

/**                   <i class="not">// NOT: /**</i>
 * This is a javadoc  <i class="not">         *This is a javadoc</i>
 * comment            <i class="not">         *comment</i>
 */                   <i class="not">         */</i>
</td>
</tr>

</table>

<h3>
<a name="Blank Lines"></a>4.5 Blank Lines
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
38. Three blank lines should be used in the following circumstances:<br />
- Between sections of a source file. <br>
- Between class and interface definitions. <br></td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
39. Two blank lines should be used between methods.</td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
40. One blank line should be used in the following circumstances:<br />
- Between the local variables in a method and its first statement. <br>
- Before a block or single-line comment. <br>
- Between logical sections inside a method, to improve readability</td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>


<!--
*******************************************************************************

   CONTROL STRUCTURES

*******************************************************************************
-->

<p>
<h1>
<a name="Control Structures"></a>5 Control Structures
</h1>

<h3>
<a name="if"></a>5.1 if
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
41. The <em>if-else</em> class of statements should have the following form:
</td>
</tr>

<tr>
<td class="example">if (condition) {
    statements;
}

if (condition) {
    statements;
} else {
    statements;
}

if (condition) {
    statements;
} else if (condition) {
    statements;
} else {
    statements;
}
</td>
</tr>

</table>

<h3>
<a name="for"></a>5.2 for
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
42. The <em>for</em> statement should have the following form:
</td></tr>

<tr>
<td class="example">for (initialization; condition; update) {
    statements;
}
</td>
</tr>

</table>

<h3>
<a name="while"></a>5.3 while
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
43. The <em>while</em> statement should have the following form:
</td>
</tr>

<tr>
<td class="example">while (condition) {
    statements;
}
</td>
</tr>

</table>

<h3>
<a name="do-while"></a>5.4 do-while
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
44. The <em>do-while</em> statement should have the following form:
</td>
</tr>

<tr>
<td class="example">do {
    statements;
} while (condition);
</td>
</tr>

</table>

<h3>
<a name="switch"></a>5.4 switch
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">

<tr>
<td class="recom">
45. The <em>switch</em> statement should have the following form:
</td>
</tr>

<tr>
<td class="example">switch (condition) {
    case ABC:
        statements;
        // falls through

    case DEF:
        statements;
        break;

    case XYZ:
        statements;
        break;

    default:
        statements;
        break;
}
</td>
</tr>

</table>

<h3>
<a name="try-catch"></a>5.5 try-catch
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
46. A <em>try-catch</em> statement should have the following form:

</td></tr>

<tr>
<td class="example">try {
    statements;
} catch (Exception exception) {
    statements;
}

try {
    statements;
} catch (Exception exception) {
    statements;
} finally {
    statements;
}
</td>
</tr>

</table>



<!--
*******************************************************************************

   6 COMMENTS

*******************************************************************************
-->


<p>
<h1>
<a name="Comments"></a>6 Comments
</h1>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
47. All comments should be written in English.
</td></tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
48. Comments should not be used to compensate for or explain bad code. Tricky or bad code should be rewritten.
</td></tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
49. There should be a white space after any comment identifier.
</td></tr>

<tr>
<td class="example">// This is a comment    <i class="not">NOT: //This is a comment</i>

/**                     <i class="not">NOT: /**</i>
 * This is a javadoc    <i class="not">      *This is a javadoc</i>
 * comment              <i class="not">      *comment</i>
 */                     <i class="not">      */</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
50. Comments should be indented relative to their position in the code.
</td></tr>

<tr>
<td class="example">while (true) {       <i class="not">// NOT: while (true) { </i>

    // Do something    <i class="not">      // Do something</i>
    something();       <i class="not">          something();</i>
}                    <i class="not">        }</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
51. Javadoc comments should have the following form:
</td></tr>

<tr>
<td class="example"><i class="not">/**
 * Return lateral location of the specified position.
 * If the position is unset, NaN is returned.
 *
 * @param x    X coordinate of position.
 * @param y    Y coordinate of position.
 * @param zone Zone of position.
 * @return     Lateral location.
 * @throws IllegalArgumentException  If zone is <= 0.
 */</i>
public double computeLocation(double x, double y, int zone)
  throws IllegalArgumentException
{
  ...
}

</td>
</tr>

</table>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
52. <em>//</em> should be used for all non-Javadoc comments, including multi-line comments.
</td></tr>

<tr>
<td class="example">// Comment spanning
// more than one line.
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
53. All public classes and all public and protected methods within public classes should be documented using Javadoc conventions.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
54. If a collection of objects can not be qualified with its type, it should be followed by a comment stating the type of its elements.
</td></tr>

<tr>
<td class="example">private Vector points; <i class="not">// of Point</i>

private Set shapes; <i class="not">// of Shape</i>

</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
55. For separation comments within differents parts of a file, the following forms should be used (depending on its level of importance):
</td></tr>

<tr>
<td class="example">//*********************
//
//*********************

//---------------------
//
//---------------------
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
56. <em>TODO</em> and <em>FIXME</em> must be written all in capitals and followed by a colon.
</td></tr>

<tr>
<td class="example">// TODO: Calculate the new order             <i class="not">// NOT: // TODO -> Calculate the new order</i>

// FIXME: Fix the synchronization algorithm  <i class="not">// NOT: fixme: Fix the synchronization algorithm</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
57. The <em>TODO</em> comment should be used to indicate pending tasks, code that is temporary, a short-term solution or good enough but not perfect code.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
58. The <em>FIXME</em> comment should be used to flag something that is bogus and broken.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<!--
*******************************************************************************

   7 LOGGING

*******************************************************************************
-->

<p>
<h1>
<a name="Logging"></a>7 Logging
</h1>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
59. All logs should be written in English.
</td>
</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
60. The use of logs in release should be strictly restricted to the necessary ones.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
61. Logs should be terse but still understandable.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
62. Logs must never contain private information or protected content.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
63. <em>System.out.println()</em> or <em>printf</em> (in native code) must never be used.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
64. The <em>ERROR</em> level should only be used when something fatal has happened.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
65. The <em>WARNING</em> level should be used when something serious and unexpected happened.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
66. The <em>INFORMATIVE</em> level should be used to note something interesting to most people.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
67. The <em>DEBUG</em> level should be used to note what is happening on the device that could be relevant to investigate and debug unexpected behaviours.
</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
68. The <em>VERBOSE</em> level should be used for everything else.</td></tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
69. A <em>DEBUG</em> log should be inside an if (LOCAL_LOGD) block.
</td></tr>

<tr>
<td class="example">if (LOCAL_LOGD) {
    Timber.d("Debugging application");
}
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>

<table class="rule" width="100%">
<tr><td class="recom">
70. A <em>VERBOSE</em> log should be inside an if (LOCAL_LOGV) block.
</td></tr>

<tr>
<td class="example">if (LOCAL_LOGV) {
    Timber.v("Infroming about current state");
}
</td>
</tr>

</table>


<!--
*******************************************************************************

   8 FILE ORGANIZATION

*******************************************************************************
-->

<p>
<h1>
<a name="File Organization"></a>8 File Organization
</h1>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
71. The <tt>package</tt> statement must be the first statement of the file.
</td></tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>
<td class="recom">
72. The <tt>import</tt> statements must follow the <tt>package</tt> statement.
<tt>import</tt> statements should be sorted by importance, grouped together by packages and leaving one blank line between groups. <br />
The ordering of packages according to their importance is as follows:
<ul>
	<li>Android imports</li>
	<li>Third parties imports (com, junit, net, org)</li>
	<li>Java imports (java and javax)</li>
</ul>
Within each group, the order of imports is alphabetical, considering that capital letters come before lower letters (e.g. Z before a).

</td></tr>

<tr>
<td class="example">import android.widget.TextView;
import android.widget.ToggleButton;

import com.ichi2.utils.DiffEngine;
import com.tomgibara.android.veecheck.util.PrefSettings;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
</td>

</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
73. Imported classes should always be listed explicitly.
</td>
</tr>

<tr>
<td class="example">import android.app.Activity;      <i class="not">// NOT: import android.app.*;</i>
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
74. Class and Interface declarations should be organized in the
following manner:
<OL>
<li> Class/Interface documentation. </li>

<li> <tt>class</tt> or <tt>interface</tt> statement.  </li>
<li> Class (<tt>static</tt>) variables in the order <tt>public</tt>,
     <tt>protected</tt>, package (no access modifier), <tt>private</tt>.</li>

<li> Instance variables in the order <tt>public</tt>,
      <tt>protected</tt>, package (no access modifier), <tt>private</tt>.</li>
<li> Constructors. </li>
<li> Methods. </li>
<li> Inner classes. </li>
</OL>
</td>

</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
75. Android Components (<em>Activity</em>, <em>Service</em>, <em>BroadcastReceiver</em> and <em>ContentProvider</em>) declarations should be organized in the
following manner:
<OL>
<li> Component documentation. </li>

<li> <tt>class</tt> statement.  </li>
<li> Class (<tt>static</tt>) variables in the order <tt>public</tt>,
     <tt>protected</tt>, package (no access modifier), <tt>private</tt>.</li>

<li> Instance variables in the order <tt>public</tt>,
      <tt>protected</tt>, package (no access modifier), <tt>private</tt>.</li>
<li> Constructors. </li>
<li> Lifecycle methods (ordered following the natural lifecycle, from creation to destruction). </li>
<li> Other methods. </li>
<li> Inner classes. </li>
</OL>
</td>

</tr>

<tr>
<td class="example"></td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr><td class="recom">
76. Methods should be vertically ordered following the two following criteria:
</td>

</tr>

<tr>
<td class="example">- Dependency: If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible.<br/>
- Conceptual Affinity: Methods that perform similar tasks or have similar naming should be vertically close.</td>
</tr>

</table>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
77. Method modifiers should be given in the following order:<br>
- access modifier: <em>public</em>, <em>protected</em> or <em>private</em><br/>
- <em>abstract</em><br/>
- <em>static</em><br/>
- <em>final</em><br/>
- <em>transient</em><br/>
- <em>volatile</em><br/>
- <em>synchronized</em><br/>
- <em>native</em><br/>
- <em>strictfp</em>
</td>
</tr>

<tr>
<td class="example">public static double square(double a);  <i class="not">// NOT: static public double square(double a);</i>
</td>
</tr>

</table>

<!--
*******************************************************************************

   9 MISCELLANEOUS

*******************************************************************************
-->

<p>
<h1>
<a name="Miscellaneous"></a>9 Miscellaneous
</h1>

<h3>
<a name="general"></a>9.1 General
</h3>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
78. Each declaration should take up an entire line.
</td>
</tr>

<tr>
<td class="example">int level;  <i class="not">// NOT: int level, size;</i>
int size;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
79. Each statement should take up an entire line.
</td>
</tr>

<tr>
<td class="example">i++;  <i class="not">// NOT: i++; j++;</i>
j++;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
80. Static variables or methods must always be accessed through the class name and never through an instance variable.
</td>
</tr>

<tr>
<td class="example">AClass.classMethod();  <i class="not">// NOT: anObject.classMethod();</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
81. The incompleteness of split lines must be made obvious.
</td>
</tr>

<tr>
<td class="example">totalSum = a + b + c +
        d + e;
        
method(param1, param2, 
        param3);
        
setText("Long line split" + 
        "into two parts."); 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
82. Special characters like TAB and page break must be avoided.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<h3>
<a name="types"></a>9.2 Types
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
83. Type conversions must always be done explicitly. Never rely on implicit type conversion.
</td>
</tr>

<tr>
<td class="example">floatValue = (int) intValue;  <i class="not">// NOT: floatValue = intValue;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
84. Arrays should be declared with their brackets next to the type.
</td>
</tr>

<tr>
<td class="example">int[] points = new int[20];  <i class="not">// NOT: int points[] = new int[20];</i>
</td>
</tr>

</table>

<h3>
<a name="variables and constants"></a>9.3 Variables and Constants
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
85. Variables should be initialized where they are declared and they should be declared in the smallest scope possible. 
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
86. Variables must never have dual meaning.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
87. Floating point variables should always be written with decimal point and at least one decimal.
</td>
</tr>

<tr>
<td class="example">double total = 0.0;    <i class="not">// NOT: double total = 0;</i> 
double speed = 3.0e8;  <i class="not">// NOT: double speed = 3e8;</i> 

double sum; 
... 
sum = (a + b) * 10.0; 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
88. Floating point variables should always be written with a digit before the decimal point.
</td>
</tr>

<tr>
<td class="example">double probability = 0.5;  <i class="not">// NOT: double probability = .5;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
89. Numerical constants (except, in some cases, -1, 0 and 1) should not be coded directly. Use constants instead.
</td>
</tr>

<tr>
<td class="example">private static final int TEAM_SIZE = 11;
...
Player[] players = new Player[TEAM_SIZE];  <i class="not">// NOT: Player[] players = new Player[11];</i>
</td>
</tr>

</table>

<h3>
<a name="operators"></a>9.4 Operators
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
90. Embedded assignments must be avoided.
</td>
</tr>

<tr>
<td class="example">a = b + c;  <i class="not">// NOT: d = (a = b + c) + r;</i>
d = a + r;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
91. The assignment operator should not be used in a place where it can be easily confused with the equality operator.
</td>
</tr>

<tr>
<td class="example"><i class="not">// NOT:
if (c++ = d++) {
    ...
}</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
92. Parenthesis should be used liberally in expressions involving mixed operators in order to make the precedence clear.
</td>
</tr>

<tr>
<td class="example">if ((a == b) && (c == d))  <i class="not">// NOT: if (a == b && c == d)
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
93. If an expression containing a binary operator appears before the ? in the ternary ?: operator, it should be parenthesized.
</td>
</tr>

<tr>
<td class="example">(x >= 0) ? x : -x;  <i class="not">// NOT: x >= 0 ? x : -x;
</td>
</tr>

</table>


<h3>
<a name="conditionals"></a>9.5 Conditionals
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
94. Complex conditional expressions must be avoided. Introduce temporary boolean variables instead.
</td>
</tr>

<tr>
<td class="example">bool isFinished = (elementNo < 0) || (elementNo > maxElement);
bool isRepeatedEntry = elementNo == lastElement; 
if (isFinished || isRepeatedEntry) {
    ... 
} 

<i class="not">// NOT: 
if ((elementNo < 0) || (elementNo > maxElement)|| elementNo == lastElement) {
    ... 
}</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
95. In an <em>if</em> statement, the normal case should be put in the <em>if</em>-part and the exception in the <em>else</em>-part
</td>
</tr>

<tr>
<td class="example">boolean isOk = openDatabase(databasePath);
if (isOk) {
    ... 
} else { 
    ... 
} 
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
96. Executable statements in conditionals should be avoided.
</td>
</tr>

<tr>
<td class="example">InputStream stream = File.open(fileName, "w");
if (stream != null) {
    ...
} 

<i class="not">// NOT: 
if (File.open(fileName, "w") != null)) {
    ... 
}</i> 
</td>
</tr>

</table>

<h3>
<a name="loops"></a>9.6 Loops
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
97. Only loop control statements must be included in the <em>for()</em> construction.
</td>
</tr>

<tr>
<td class="example">maxim = -1;                               <i class="not">// NOT: for (i = 0, maxim = -1; i < 100; i++) {</i> 
for (i = 0; i < 100; i++) {			      <i class="not">maxim = max(maxim, value[i]);</i>
	maxim = max(maxim, value[i]);	          <i class="not">}</i>
}
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
98. Loop variables should be initialized immediately before the loop.
</td>
</tr>

<tr>
<td class="example">boolean isFound = false;    <i class="not">// NOT: boolean isFound = false;</i>
while (!isFound) {            <i class="not">      ...</i>
    ...                       <i class="not">      while (!isFound) {</i>
}                                <i class="not">       ...</i>
                              <i class="not">      }</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
99. <em>do-while</em> loops can be avoided.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
100. The use of <em>break</em> and <em>continue</em> in loops should be avoided. 
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<h3>
<a name="exceptions and finalizers"></a>9.7 Exceptions and Finalizers
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
101. Exceptions must not be ignored without a good explanation.
</td>
</tr>

<tr>
<td class="example"><i class="not">// NOT:
void setServerPort(String value) {
    try {
        serverPort = Integer.parseInt(value);
    } catch (NumberFormatException e) { 
    }
}</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
102. Generic Exception should not be caught except at the root of the stack.
</td>
</tr>

<tr>
<td class="example">try {
    someComplicatedIOFunction();        // may throw IOException 
    someComplicatedParsingFunction();   // may throw ParsingException 
    someComplicatedSecurityFunction();  // may throw SecurityException 
} catch (IOException exception) {                
    handleIOError();                  
} catch (ParsingException exception) {
    handleParsingError();
} catch (SecurityException exception) {
    handleSecurityError();
}	

<i class="not">// NOT:
try {
    someComplicatedIOFunction();        // may throw IOException 
    someComplicatedParsingFunction();   // may throw ParsingException 
    someComplicatedSecurityFunction();  // may throw SecurityException 
} catch (Exception exception) {
    handleError();
}</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
103. Finalizers should be avoided.
</td>
</tr>

<tr>
<td class="example">
</td>
</tr>

</table>


<h3>
<a name="android xml layouts"></a>9.8 Android XML Layouts
</h3>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
104. At the same level of the element's name, it should only appear the <em>xmlns</em> attribute or the <em>id</em> attribute, in this order.
</td>
</tr>

<tr>
<td class="example">&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/example_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    &gt;

&lt;RelativeLayout android:id="@+id/example_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    &gt;
    
&lt;RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    &gt;
    
<i class="not">// NOT:
&lt;RelativeLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    &gt;
    
&lt;RelativeLayout android:id="@+id/example_id"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    &gt;</i>
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
105. Each attribute should take up an entire line.
</td>
</tr>

<tr>
<td class="example">&lt;EditText android:id="@+id/answer_field"      	    <i class="not">// NOT: &lt;EditText android:id="@+id/answer_field"</i>
    android:layout_width="fill_parent"				<i class="not">android:layout_width="fill_parent" android:layout_height="wrap_content"</i>
    android:layout_height="wrap_content"	                <i class="not">android:maxLines="2" android:visibility="gone"/&gt;</i>
    android:maxLines="2"
    android:visibility="gone"
    /&gt;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
106. Attributes should be grouped by type (layout properties, text properties…) and within the same type, they should be ordered alphabetically.
</td>
</tr>

<tr>
<td class="example">&lt;TextView android:id="@+id/example_id"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textColor="#ffffff"
    android:textSize="24sp"
    /&gt;

<i class="not">// NOT:
&lt;TextView android:id="@+id/example_id"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:layout_width="fill_parent"
    android:textSize="24sp"
    /&gt;
    
&lt;TextView android:id="@+id/example_id"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textSize="24sp"
    android:textColor="#ffffff"
    /&gt;
</td>
</tr>

</table>

<!--
****************************************************************************
   Rule
****************************************************************************
-->
<p>
<table class="rule" width="100%">
<tr>

<td class="recom">
107. <em>LinearLayout</em> elements must explicity state the <em>orientation</em> attribute.
</td>
</tr>

<tr>
<td class="example">&lt;LinearLayout                                   <i class="not">// NOT: &lt;LinearLayout</i>
    android:layout_width="fill_parent"                      <i class="not">android:layout_width="fill_parent"</i>
    android:layout_height="wrap_content"                    <i class="not">android:layout_height="wrap_content"</i>
    android:orientation="horizontal"                        <i class="not">&gt;</i>
    &gt;	
</td>
</tr>

</table>


<!--
*******************************************************************************

   10 CHEAT SHEET

*******************************************************************************
-->

<p>
<h1>
<a name="Cheat Sheet"></a>10 Cheat Sheet
</h1>

<table border="1" width="100%" cellpadding="5px">
<tr>
<th>What</th>
<th>Naming Convention</th>
<th>GOOD Examples</th>
<th>BAD Examples</th>
</tr>
<tr>
<td>Package</td>
<td>lower case</td>
<td>com.company.application.ui</td>
<td>com.company.Application.Ui</td>
</tr>
<tr>
<td>Class</td>
<td>nouns, mixed case, starting with uppercase</td>
<td>AudioSystem</td>
<td>audioSystem</td>
</tr>
<tr>
<td>Variable</td>
<td>mixed case, starting in lowercase</td>
<td>availableWidth</td>
<td>AvailableWidth, available_width</td>
</tr>
<td>Non-public, non-static field</td>
<td>mixed case, starting with "m"</td>
<td>mLongVariable</td>
<td>longVariable, LongVariable</td>
</tr>
<td>Static field</td>
<td>mixed case, starting with "s"</td>
<td>sSingleton</td>
<td>singleton, staticVariable</td>
</tr>
<tr>
<td>Constant</td>
<td>uppercase, using underscore to separate words</td>
<td>SOME_CONSTANT</td>
<td>some_constant</td>
</tr>
<tr>
<td>Method</td>
<td>verbs, mixed case, starting with lowercase</td>
<td>getName()</td>
<td>get_name()</td>
</tr>
<td>String key</td>
<td>lowercase, using underscore to separate words</td>
<td>good_example_key</td>
<td>badExampleKey</td>
</tr>
<td>XML element identifier</td>
<td>lowercase, using underscore to separate words</td>
<td>good_id_example</td>
<td>badIdExample</td>
</tr>
</table> 

<p>

<table width="100%" cellspacing="10px">
<tr valign="top">
<td width="20%"><b>Line length</b></td>
<td width="80%"><b>120</b> characters</td>
</tr>
<tr valign="top">
<td width="20%"><b>Indentation</b></td>
<td width="80%">
<b>Normal:</b> <b>4</b> spaces with no tabs<br/>
<b>Wrapping lines:</b> <b>8</b> spaces with no tabs</td>
</tr>
<tr valign="top">
<td width="20%"><b>Braces style</b></td>
<td width="80%"><em>1TBS</em> braces style: class declarations, method declarations, block statements</td>
</tr>
<tr valign="top">
<td width="20%"><b>White spaces</b></td>
<td width="80%">
<b>Before and after:</b> operators<br/>
<b>Before:</b> opening brace<br/>
<b>After:</b> Java reserved words, commas, semicolons in for statements, any comment identifier
</td>
</tr>
<tr valign="top">
<td width="20%"><b>Blank lines</b></td>
<td width="80%">
<b>3 blank lines:</b><br/>
Between sections of a source file<br/>
Between class and interface definitions<br/>
<br/>
<b>2 blank lines:</b><br/>
Between methods<br/>
<br/>
<b>1 blank line:</b><br/>
Between local variables in a method and its first statement<br/>
Before a block or single-line comment<br/>
Between logical sections inside a method
</td>
</tr>
<tr valign="top">
<td width="20%"><b>File organization</b></td>
<td width="80%">Copyright/ID comment<br/>package declaration<br/>import statements</td>
</tr>
<tr valign="top">
<td width="20%"><b>Class organization</b></td>
<td width="80%">
Class/Interface documentation<br/>
<em>class</em> or <em>interface</em> statement<br/>
Class (static) variables in the order <em>public</em>, <em>protected</em>, package (no access modifier), <em>private</em><br/>
Instance variables in the order <em>public</em>, <em>protected</em>, package (no access modifier), <em>private</em><br/>
Constructors<br/>
Methods<br/>
Inner classes
</td>
</tr>
<tr valign="top">
<td width="20%"><b>Android components organization</b></td>
<td width="80%">
Component documentation<br/>
<em>class</em> statement<br/>
Class (static) variables in the order <em>public</em>, <em>protected</em>, package (no access modifier), <em>private</em><br/>
Instance variables in the order <em>public</em>, <em>protected</em>, package (no access modifier), <em>private</em><br/>
Constructors<br/>
Lifecycle methods (ordered following the natural lifecycle, from creation to destruction)<br/>
Other methods<br/>
Inner classes
</td>
</tr>
<tr valign="top">
<td width="20%"><b>Order of imports</b></td>
<td width="80%">Android imports, third parties imports (alphabetically), Java imports (java and javax)</td>
</tr>
<tr valign="top">
<td width="20%"><b>Order of method modifiers</b></td>
<td width="80%"><em>public</em> <em>protected</em> <em>private</em> <em>abstract</em> <em>static</em> <em>final</em> <em>transient</em> <em>volatile</em> <em>synchronized</em> <em>native</em> <em>strictfp</em></td>
</tr>
</table>

<!--
*******************************************************************************

   11 REFERENCES

*******************************************************************************
-->

<p>
<h1>
<a name="References"></a>11 References

</h1>

<p>
<a name="ref1"></a>
[1] Code Complete, Steve McConnel - Microsoft Press

<p>
<a name="ref2"></a>
[2] Clean Code, Robert C. Martin - Prentice Hall

<p>
<a name="ref3"></a>
[3] Java Coding Style Guide, Achut Reddy - Sun Microsystems, Inc.<br>
<a href="http://cs.bilgi.edu.tr/pages/standards%5Fproject/java%5FCodingStyle.pdf">
http://cs.bilgi.edu.tr/pages/standards%5Fproject/java%5FCodingStyle.pdf</a>

<p>
<a name="ref4"></a>
[4] Java Code Conventions<br>
<a href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html</a>

<p>
<a name="ref5"></a>
[5] Android Code Style Guidelines for Contributors <br>
<a href="http://source.android.com/source/code-style.html">
http://source.android.com/source/code-style.html</a>

<p>
<a name="ref6"></a>
[6] Coding Standards for Java from Geosoft<br>
<a href="http://geosoft.no/development/javastyle.html">
http://geosoft.no/development/javastyle.html</a>



<!--
*******************************************************************************
   Footer
*******************************************************************************
-->

<br>
<br>

<hr size=1>

<table width="100%">
  <tr>
    <td>
      <center class=footnote>

        Edu Zamora, AnkiDroid &copy; 2010<br>
        This page is maintained by
        <a href="mailto:ankidroid@gmail.com">ankidroid@gmail.com</a>
      </center>
    </td>
  </tr>
</table>


</body>
</html>
